<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>优惠券管理</el-breadcrumb-item>
      <!-- <el-breadcrumb-item>优惠券列表</el-breadcrumb-item> -->
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="5">
          <el-button type="primary" @click="toAddCoupon">添加优惠券</el-button>
        </el-col>
      </el-row>

      <!-- 用户列表区域 -->
      <el-table :data="couponList" border stripe>
        <!-- <el-table-column type="index"></el-table-column> -->
        <el-table-column label="名称" min-width="10%" prop="name"></el-table-column>
        <el-table-column label="额度" min-width="10%" prop="quota"></el-table-column>
        <el-table-column label="兑换积分" min-width="5%" prop="integral"></el-table-column>
        <el-table-column label="简介" min-width="55%" prop="explain"></el-table-column>
        <el-table-column label="操作" min-width="15%">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="toUpCoupon(scope.row.id)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeCouponById(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      couponList: [],
    };
  },
  created() {
    //获取优惠券列表
    this.getCouponList();
  },
  methods: {
    //获取优惠券列表(调用后台接口)
    async getCouponList() {
      const { data: res } = await this.$http.get("coupon/listCoupon2", {
        params: this.queryInfo,
      });

      if (res.code !== 200) {
        return this.$message.error("获取优惠券列表失败！");
      }
      this.couponList = res.data.listCouponVOs;
    },
    toAddCoupon() {
      this.$router.replace("/coupon/addCoupon");
    },
    async toUpCoupon(id) {

      this.$router.push("/coupon/upCoupon/" + id);
    },
    // 根据Id删除对应的用户信息
    async removeCouponById(id) {

      // 弹框询问用户是否删除数据
      const confirmResult = await this.$confirm(
        "此操作将永久删除该优惠券, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);

      // 如果用户确认删除，则返回值为字符串 confirm
      // 如果用户取消了删除，则返回值为字符串 cancel

      if (confirmResult !== "confirm") {
        return this.$message.info("已取消删除");
      }

      let params = new FormData();
      params.append("ids", id);

      //调用删除接口
      const { data: res } = await this.$http.post(
        "coupon/deleteCoupon",
        params
      );

      if (res.code !== 200) {
        this.$message.error(res.massage);
        this.getCouponList();
        return;
      }

      this.$message.success("删除优惠券成功！");
      this.getCouponList();
    },
  },
};
</script>

<style lang="less" scoped>
.button:hover {
  background-color: rgb(64, 158, 255);
  color: rgb(255, 255, 255);
}
</style>
